#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumNumbers(TreeNode* root) {
        int sum = 0;
        string val;
        val += root->val + 48;
        Sum(root, val, sum);

        return sum;
    }
    void Sum(TreeNode* root, string val, int& sum)
    {
        if (root->right == nullptr && root->left == nullptr)
        {
            sum += stoi(val);
            cout << root->val << endl;
            cout << sum << endl;;
            cout << val.c_str() << endl << endl;;
            return;
        }
        if (root->left)
        {
            val += root->left->val + 48;
            Sum(root->left, val, sum);
            val.pop_back();
        }
        if (root->right)
        {
            val += root->right->val + 48;
            Sum(root->right, val, sum);
            val.pop_back();
        }

    }
};